package com.tencent.wxcloudrun.controller.OrderGoods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencent.wxcloudrun.model.OrderGoods;
import com.tencent.wxcloudrun.response.R;
import com.tencent.wxcloudrun.service.OrderGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: OrderGoodsController
 * Package: com.tencent.wxcloudrun.controller
 * Description:
 *
 * @Date: 2023/1/22 8:34 AM
 * @Author: wht
 */
@Slf4j
@RestController
@RequestMapping("/orderGoods")
public class OrderGoodsController {
    @Autowired
    OrderGoodsService orderGoodsService;

    @PutMapping("/addOne")
    public <T> R<T> addOne(@RequestBody OrderGoods orderGoods) {
        boolean res = orderGoodsService.save(orderGoods);

        if (res) {
            return R.AddSuccess();
        } else {
            return R.AddFail();
        }
    }

    @DeleteMapping("/delOne")
    public <T> R<T> delOne(@RequestParam(required = true, value = "id") Integer id) {
        boolean res = orderGoodsService.removeById(id);

        if (res) {
            return R.DelSuccess();
        } else {
            return R.DelFail();
        }
    }

    @PatchMapping("/updateOne")
    public <T> R<T> updateOne(@RequestBody OrderGoods orderGoods) {
        boolean res = orderGoodsService.updateById(orderGoods);

        if (res) {
            return R.UpdateSuccess();
        } else {
            return R.UpdateFail();
        }
    }

    @GetMapping("/getOne")
    public <T> R<T> getOne(@RequestParam(required = true, value = "id") Integer id) {
        OrderGoods orderGoods = orderGoodsService.getById(id);
        return R.successData(orderGoods);
    }

    @GetMapping("/getList")
    public <T> R<T> getList(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize
    ) {
        Page<OrderGoods> page = new Page<>(pageNum, pageSize);
        List<OrderGoods> list = orderGoodsService.list(page);
        return R.successData(list);
    }
}
